/**
 * 2010-4-29
 * InnovationServer
 * @author lyh
 */
package labox.innovation.gameserver.model.quest.dataset;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import javolution.util.FastMap;
import labox.innovation.config.Config;
import labox.innovation.gameserver.model.quest.Quest;
import labox.innovation.gameserver.model.quest.dataset.QuestDataSet.QuestTypeEnum;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * ����ģ��XML����
 * 
 * @author lyh
 * 
 */
public final class JQuestXMLParse {

	private static final Logger _log = LoggerFactory.getLogger(JQuestXMLParse.class.getName());

	static Map<Integer, QuestDataSet> _questDataSet = new FastMap();

	private JQuestXMLParse() {
		try {
			parseExcelTemplet(new File(Config.DATAPACK_ROOT, "/data/questScriptTemplet.xml"));
			_log.info("Parse Quest Script Template Success", Config.SERVER_ID, "�������");
		} catch (Exception e) {
			_log.error("Parse Quest Script Template Error", Config.SERVER_ID, e);
		}

	}

	private static class SingletonHolder {
		protected static final JQuestXMLParse _instance = new JQuestXMLParse();
	}

	public static JQuestXMLParse getInstance() {
		return SingletonHolder._instance;
	}

	/**
	 * ��������XMLģ��
	 * 
	 * @param file
	 * @throws IOException
	 *             2010-4-21
	 * @author lyh
	 */
	public void parseExcelTemplet(File file) throws Exception {
		DocumentBuilder db = null;
		Document doc = null;

		db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		doc = db.parse(file);

		Element root = doc.getDocumentElement(); // rootΪ"QuestTemplate"�ڵ�
		NodeList questDS = root.getElementsByTagName("Quest"); // ����"quest"�ڵ㼯��
		Method methods;
		for (int i = 0; i < questDS.getLength(); i++) {
			Element questDSs = (Element) questDS.item(i);
			NodeList childNodes = questDSs.getChildNodes();
			QuestDataSet dataSet = new QuestDataSet();
			for (int j = 0; j < childNodes.getLength(); j++) {
				Node node = (Node) childNodes.item(j);
				String temp = node.getTextContent();
				try {
					if (node.getNodeName().equals("#text"))
						continue;
					methods = QuestDataSet.class.getMethod(toSetMethod(node.getNodeName()), String.class);
					if (temp == null || temp.equals(""))
						temp = "-1";
					methods.invoke(dataSet, temp);
				} catch (Exception e) {
					_log.error("����:" + node.getNodeName() + ",ֵ:" + node.getTextContent() + ",�������", Config.SERVER_ID, e);
				}
			}
			Class questClass = QuestHandle.getInstance().getQuestClass(QuestTypeEnum.getQuestTypeEnum(dataSet.getQuestType()));
			if (questClass == null)
				_log.error("�������:questType=" + dataSet.getQuestType() + ",��" + i + "������,����û��ƥ�������ģ��", Config.SERVER_ID, "�������");
			Quest quest;
			try {
				quest = (Quest) questClass.getConstructor(int.class, String.class, String.class).newInstance(dataSet.getId(), dataSet.getQn(), dataSet.getName());
				quest.putQuestDataSet(dataSet);
				quest.initQuest();
			} catch (Exception e) {
				_log.error("error init quest id:" + dataSet.getId(), Config.SERVER_ID, e);
			}
		}
	}

	public static void main(String[] args) {

		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = null;
		try {
			db = dbf.newDocumentBuilder();
		} catch (Exception e) {
			System.err.println(e);
			System.exit(1);
		}

		Document doc = null;
		try {
			doc = db.parse("D:\\WorkSpack2\\InnovationRes\\data\\questScriptTemplet.xml");
		} catch (Exception e) {
			System.err.println(e);
			System.exit(1);
		}
		Element root = doc.getDocumentElement(); // rootΪ"QuestTemplate"�ڵ�
		NodeList questDS = root.getElementsByTagName("Quest"); // ����"quest"�ڵ㼯��
		// Ϊÿһ�������ѧ����Ϣ������Ӧ��QuestDataSet�������ö�����Ϣ
		for (int i = 0; i < questDS.getLength(); i++) {
			Element questDSs = (Element) questDS.item(i);
			System.out.println(questDSs.getNodeName());
			NodeList childNodes = questDSs.getChildNodes();
			for (int j = 0; j < childNodes.getLength(); j++) {
				Node node = (Node) childNodes.item(j);
				System.out.println(node.getNodeName());
				System.out.println(node.getTextContent());

			}
			// // ����ʵ�ֲ��Ǻܺ��?java��nodelistû�и��tagѰ���ӽڵ�ķ������Ժ�����
			// studentBean.setName(student.getChildNodes().item(1).getTextContent());
			// // item(3)�õ�����ѧ����Ա�ڵ�,java��</����>Ҳ��Ϊ��һ���ӽڵ㴦��
			// studentBean.setSex(student.getChildNodes().item(3).getTextContent());
			// // ����ѧ�����vector������
			// student_vector.add(studentBean);
		}

	}

	private String toSetMethod(String s) {
		char[] ch = s.toCharArray();
		ch[0] = (char) (ch[0] - 32);
		String col = "set" + String.valueOf(ch);
		return col;
	}

}
